
/**   
 * @Title: AppConfigManager.java 
 * @Package: com.rename.icode 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-11 下午5:08:52 
 * @version  
 */


package com.rename.icode.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.rename.icode.comm.ApplicationContext;
import com.rename.icode.comm.Constants;
import com.rename.icode.model.LoginConfig;
import com.rename.icode.model.Server;

/** 
 * @Description 保存登录配置信息、服务器配置信息
 * @author Administrator
 * @date 2015-2-11 下午5:08:52 
 * @version V1.0
 */

public class AppConfigManager {
	private static final String TAG = "AppConfigManager";
	private SharedPreferences mAppPref;
	private Context mCntx;

	private LoginConfig mLoginCfg;

	private static class Loader {
        static AppConfigManager INSTANCE = new AppConfigManager();
    }
	 
	public static AppConfigManager getInstance() {
		return Loader.INSTANCE;
	}
	
	private AppConfigManager() {
		mCntx = ApplicationContext.get();
		mAppPref = mCntx.getSharedPreferences(Constants.APP_SET_PREF, 0);

		loadLoginConfig();
	}
	
	public synchronized SharedPreferences getAppPref() {
		return mAppPref;
	}
	
	public synchronized void saveLoginConfig() {//注册或者登录成功后调用，保存登录信息
		//服务器信息
		mAppPref.edit().putString(Constants.APP_HOST, mLoginCfg.getServer().getHost()).commit();
		mAppPref.edit().putInt(Constants.APP_PORT, mLoginCfg.getServer().getPort()).commit();
		
		//用户信息
		mAppPref.edit().putString(Constants.USERNAME, mLoginCfg.getUsername()).commit();
		mAppPref.edit().putString(Constants.PASSWORD, mLoginCfg.getPassword()).commit();
		mAppPref.edit().putString(Constants.DATA_ROOT_PATH, mLoginCfg.getRootPath()).commit();
		
	}
	
	public synchronized void loadLoginConfig() {//当sharedpreferences中有登录信息后再调用
		mLoginCfg = null;
		mLoginCfg = new LoginConfig();
		
		Server srv = new Server();
		srv.setHost(mAppPref.getString(Constants.APP_HOST, null));
		srv.setPort(mAppPref.getInt(Constants.APP_PORT, -1));

		mLoginCfg.setServer(srv);
		mLoginCfg.setUsername(mAppPref.getString(Constants.USERNAME, null));
		mLoginCfg.setPassword(mAppPref.getString(Constants.PASSWORD, null));
		mLoginCfg.setRootPath(mAppPref.getString(Constants.DATA_ROOT_PATH, null));
	}
	
	public synchronized void setRootPath(String path) {
		mLoginCfg.setRootPath(path);
	}
	
	public synchronized String getRootPath() {
		return mLoginCfg.getRootPath();
	}

	public synchronized void setUsername(String name) {
		mLoginCfg.setUsername(name);
	}

	public synchronized void setPassword(String pwd) {
		mLoginCfg.setPassword(pwd);
	}
	  	
	public synchronized String getUsername() {
		return mLoginCfg.getUsername();
	}
	  	
	public synchronized String getPassword() {
		return mLoginCfg.getPassword();
	}
}
